﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    initUI();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initUI()
{
    this->setWindowTitle("PcmPlayer");
    this->resize(410, 190);

    mode = PlayMode::None;
}

void Widget::on_btnPcmFile_clicked()
{
    QString fileUrl = QFileDialog::getOpenFileName(this, "Open PcmFile", "./", "PCM Files (*.pcm)");
    if(fileUrl.isEmpty())
        return;

    ui->labSrc->setText(fileUrl);
    mode = PlayMode::PcmFile;
}

void Widget::on_btnAudioDevice_clicked()
{
    QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
    if(devices.isEmpty())
        return;

    QString deviceUrl;
    foreach(auto temp, devices)
    {
        deviceUrl = temp.deviceName();
        break;
    }

    ui->labSrc->setText(deviceUrl);
    mode = PlayMode::Device;
}

void Widget::on_btnStart_clicked()
{
    QString url = ui->labSrc->text();
    if(url.isEmpty())
        return;

    if(audioPlayer.isRunState())
        return;

    int sampleRate = ui->cbBoxSampleRate->currentText().toInt();
    int sampleSize = ui->cbBoxSampleSize->currentText().toInt();
    int channel = ui->cbBoxChannel->currentText().toInt();

    audioPlayer.setAudioParm(mode, url, sampleRate, sampleSize, channel);
    audioPlayer.startPlay();
}

void Widget::on_btnStop_clicked()
{
    audioPlayer.stopPlay();
}
